package com.aurora.oa.mycenter.view;

import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

import com.aurora.oa.Constants.AuroraConstants;
import com.aurora.oa.R;
import com.aurora.oa.activity.BaseActivity;
import com.aurora.oa.adapter.CommonRecyclerViewAdapter;
import com.aurora.oa.adapter.CommonRecyclerViewHolder;
import com.aurora.oa.customview.ItemDivider;
import com.aurora.oa.listener.OnAddGroupMemberListener;
import com.aurora.oa.listener.OnAddTeamMemberListener;
import com.aurora.oa.listener.OnGetMyContactsListListener;
import com.aurora.oa.listener.OnGetMyTeamMemberListListener;
import com.aurora.oa.model.RequestHttpModel;
import com.aurora.oa.model.RequestHttpModelImpl;
import com.aurora.oa.mycenter.bean.ContactsBean;
import com.aurora.oa.mycenter.bean.TeamMemberBean;
import com.aurora.oa.utils.StringUtils;
import com.aurora.oa.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 *联系人列表
 */
public class MyMemberListActivity extends BaseActivity{
    CommonRecyclerViewAdapter<ContactsBean> contactsAdapter;
    CommonRecyclerViewAdapter<TeamMemberBean> teamMemberAdapter;
    List<ContactsBean> contactsBeanList = new ArrayList<>();
    List<TeamMemberBean> teamMemberList = new ArrayList<>();
    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;
    LinearLayoutManager linearLayoutManager;
    RequestHttpModel model = new RequestHttpModelImpl();
    private static final int TEAM_MEMBER = 1;
    private static final int CONTACTS_MEMBER = 2;
    TextView tv_add_member;
    //组id
    String id;
    int Type;
    //选择了谁
    private String userids;

    /*    View view;
        int startX,startY;*/
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setMyContentView(R.layout.activity_member_list);
        ButterKnife.bind(this);
        initView();
        initData();
/*        view.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                switch (motionEvent.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        startX = (int)motionEvent.getRawX();
                        startY = (int)motionEvent.getRawY();
                        break;
                    case MotionEvent.ACTION_MOVE:
                        int moveX = (int)motionEvent.getRawX();
                        int moveY =(int)motionEvent.getRawY();

                        int disX = moveX-startX;
                        int disY = moveY -startY;

                        int left = view.getLeft()+disX;
                        int top = view.getTop() + disY;
                        int right =view.getRight() + disX;
                        int bottom = view.getBottom() + disY;
                        view.layout(left,top,right,bottom);
                        break;
                    case MotionEvent.ACTION_UP:
                        break;
                }

                return false;
            }
        });*/
    }

    private void initView() {
        //选择添加成员
        tv_add_member = (TextView) findViewById(R.id.tv_right_title);
        tv_add_member.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
        linearLayoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.addItemDecoration(new ItemDivider().setDividerWith(3).setDividerColor(Color.TRANSPARENT));
        id = getIntent().getStringExtra("id");
        Type = getIntent().getIntExtra("type",0);
        String title;
        switch (Type){
            case TEAM_MEMBER:
                title = "团队成员";
                teamMemberAdapter = new CommonRecyclerViewAdapter<TeamMemberBean>(this,teamMemberList) {
                    @Override
                    public void convert(CommonRecyclerViewHolder h, TeamMemberBean entity, int position) {
                        h.setText(R.id.tv_name,entity.username);
                        h.setImageURL(R.id.sdv_head_img, AuroraConstants.BASE_SERVER_URL+"/"+entity.headimg);
                    }

                    @Override
                    public int getLayoutViewId(int viewType) {
                        return R.layout.item_my_contacts;
                    }
                };
                teamMemberAdapter.setOnViewInItemClickListener(new CommonRecyclerViewAdapter.OnViewInItemClickListener() {
                    @Override
                    public void onViewInItemClick(View v, int position) {
                        if(StringUtils.isNullString(teamMemberList.get(position).mphone)){
                            ToastUtil.show(MyMemberListActivity.this,"此人联系电话为空");
                            return;
                        }
                        switch (v.getId()){
                            case R.id.tv_send_message:

                                Intent intent = new Intent();
                                //系统默认的action，用来打开默认的短信界面
                                intent.setAction(Intent.ACTION_SENDTO);
                                //需要发短息的号码
                                intent.setData(Uri.parse("smsto:"+teamMemberList.get(position).mphone));
                                MyMemberListActivity.this.startActivity(intent);
                                break;
                            case R.id.sdv_call_img:

                                Intent intentcall = new Intent();
                                //系统默认的action，用来打开默认的电话界面
                                intentcall.setAction(Intent.ACTION_CALL);
                                //需要拨打的号码
                                intentcall.setData(Uri.parse("tel:"+teamMemberList.get(position).mphone));
                                MyMemberListActivity.this.startActivity(intentcall);
                                break;
                        }
                    }
                },R.id.tv_send_message,R.id.sdv_call_img);
                recyclerView.setAdapter(teamMemberAdapter);
                break;
            case CONTACTS_MEMBER:
                title = "我的通讯录";
                contactsAdapter = new CommonRecyclerViewAdapter<ContactsBean>(this,contactsBeanList) {
                    @Override
                    public void convert(CommonRecyclerViewHolder h, ContactsBean entity, int position) {
                        h.setText(R.id.tv_name,entity.username);
                        h.setImageURL(R.id.sdv_head_img, AuroraConstants.BASE_SERVER_URL+"/"+entity.userHeadImage);
                    }

                    @Override
                    public int getLayoutViewId(int viewType) {
                        return R.layout.item_my_contacts;
                    }
                };
                contactsAdapter.setOnViewInItemClickListener(new CommonRecyclerViewAdapter.OnViewInItemClickListener() {
                    @Override
                    public void onViewInItemClick(View v, int position) {
                        if(StringUtils.isNullString(contactsBeanList.get(position).phone)){
                            ToastUtil.show(MyMemberListActivity.this,"此人联系电话为空");
                            return;
                        }
                        switch (v.getId()){
                            case R.id.tv_send_message:

                                Intent intent = new Intent();
                                //系统默认的action，用来打开默认的短信界面
                                intent.setAction(Intent.ACTION_SENDTO);
                                //需要发短息的号码
                                intent.setData(Uri.parse("smsto:"+contactsBeanList.get(position).phone));
                                MyMemberListActivity.this.startActivity(intent);
                                break;
                            case R.id.sdv_call_img:

                                Intent intentcall = new Intent();
                                //系统默认的action，用来打开默认的电话界面
                                intentcall.setAction(Intent.ACTION_CALL);
                                //需要拨打的号码
                                intentcall.setData(Uri.parse("tel:"+contactsBeanList.get(position).phone));
                                MyMemberListActivity.this.startActivity(intentcall);
                                break;
                        }
                    }
                },R.id.tv_send_message,R.id.sdv_call_img);
                recyclerView.setAdapter(contactsAdapter);
                break;
            default:
                title = "";
                break;
        }
        initTitlebar(true,title,true);

    }

    private void initData() {
        switch (Type){
            case TEAM_MEMBER:
                model.getMyTeamMemBerList(id,new OnGetMyTeamMemberListListener() {
                    @Override
                    public void onGetMyTeamMemberListSuccess(List<TeamMemberBean> teamMemberBeanList) {
                        refreshTeamMemberList(teamMemberBeanList);
                    }

                    @Override
                    public void onGetMyTeamMemberListFail(String message) {
                        ToastUtil.show(MyMemberListActivity.this,message);;
                    }
                });
                break;
            case CONTACTS_MEMBER:
                model.getMyContactsList(id,new OnGetMyContactsListListener() {
                    @Override
                    public void onGetMyContactsListSuccess(List<ContactsBean> contactsBeanList) {
                        refreshContactsMemberList(contactsBeanList);
                    }

                    @Override
                    public void onGetMyContactsListFail(String message) {
                        ToastUtil.show(MyMemberListActivity.this,message);;
                    }
                });
                break;
        }

    }

    private void refreshTeamMemberList(List<TeamMemberBean> teamMemberList) {
        teamMemberList.clear();
        teamMemberList.addAll(teamMemberList);
        teamMemberAdapter.notifyDataSetChanged();
    }

    private void refreshContactsMemberList(List<ContactsBean> contactsMemberList) {
        contactsMemberList.clear();
        contactsMemberList.addAll(contactsMemberList);
        contactsAdapter.notifyDataSetChanged();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (resultCode){
            case 4:
                userids = data.getExtras().getString("who_to_send");
                tv_add_member(userids);
                break;
        }

    }

    private void tv_add_member(String userids) {
        switch (Type){
            //增加团队成员
            case TEAM_MEMBER:
                model.doAddTeamMember(id,userids,new OnAddTeamMemberListener() {
                    @Override
                    public void onAddTeamMemberSuccess() {
                        initData();
                    }

                    @Override
                    public void onAddTeamMemberFail(String message) {
                        ToastUtil.show(MyMemberListActivity.this,message);
                    }
                });
                break;
            case CONTACTS_MEMBER:
                model.doAddGroupMember(id,userids,new OnAddGroupMemberListener() {
                    @Override
                    public void onAddGroupMemberSuccess() {
                        initData();
                    }

                    @Override
                    public void onAddGroupMemberFail(String message) {
                        ToastUtil.show(MyMemberListActivity.this,message);;
                    }
                });
                break;
        }
    }
}
